package com.dusk.util
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.utils.ByteArray;
	
	public final class TextUtil extends UtilBase
	{
		public function TextUtil()
		{
			throw new Error("TextUtil class is static container only");
		}
		
		public static const BOSS_BLOOD_FONT:String = "华康海报体W12";
		
		public static const UNIVERSAL_FONT:String = "FZCuYuan-M03";
		
		/**
		 * 创建新的textField
		 * @param content 内容
		 * @param font 字体，默认方正粗圆
		 * @param size 字体大小，默认20
		 * @param align 对齐方式，默认左对齐
		 * @param color 字体颜色，默认黑色
		 * @param wordWrap 自动换行，默认开启
		 * @param selectable 可选，默认关闭
		 * @return
		 */
		public static function creatTextField(content:String, font:String = UNIVERSAL_FONT, size:int = 20, align:String = TextFormatAlign.LEFT, color:uint = 0x000000, wordWrap:Boolean = true, selectable:Boolean = false):TextField
		{
			var txt:TextField = new TextField();
			var tf:TextFormat = new TextFormat();
			tf.color = color;
			tf.size = size;
			tf.font = font;
			tf.align = align;
			txt.x = txt.y = 0;
			txt.autoSize = align;
			txt.embedFonts = true;
			txt.defaultTextFormat = tf;
			txt.text = content;
			txt.setTextFormat(tf);
			txt.width = txt.textWidth + 50;
			txt.height = txt.textHeight + 50;
			txt.selectable = selectable;
			txt.wordWrap = wordWrap;
			return txt;
		}
		
		/**
		 * 克隆一个新的textfield对象
		 * @param textField 目标textfield
		 * @param font
		 * @return 新的textfield
		 */
		public static function cloneTxt(textField:TextField,font:String = UNIVERSAL_FONT):TextField
		{
			var newText:TextField = new TextField();
			var tf:TextFormat = textField.defaultTextFormat;
			if (!textField)
			{
				throw new Error("clone textField err! Target must be none null!");
			}
			tf.font = font;
			newText.name = textField.name;
			newText.embedFonts = true;
			newText.x = textField.x;
			newText.y = textField.y;
			newText.type = textField.type;
			newText.width = textField.width;
			newText.height = textField.height;
			newText.defaultTextFormat = tf;
			newText.wordWrap = textField.wordWrap;
			newText.selectable = textField.selectable;
			newText.text = textField.text;
			newText.filters = textField.filters;
			return newText;
		}
		
		public static function redrawCtrlerTxt(ctrler:DisplayObjectContainer):void
		{
			const str:String = "textField";
			if(!ctrler.hasOwnProperty(str))
			{
				return;
			}
			if(!ctrler[str].hasOwnProperty("getTextFormat"))
			{
				return;
			}
			var tf:TextFormat = ctrler[str].getTextFormat();
			ctrler[str].embedFonts = true;
			tf.font = BOSS_BLOOD_FONT;
			ctrler[str].setStyle(str,tf);
		}
		
		/**
		 * 重绘显示对象内的文字对象，请在super()实例化父类后调用
		 *
		 */
		public static function redrawTxt(container:DisplayObjectContainer):void
		{
			if(!container)
			{
				throw new Error("redraw textField err! Container must be none null");
			}
			for (var idx:int = 0; idx < container.numChildren; idx++)
			{
				var obj:DisplayObject = container.getChildAt(idx);
				if(obj is TextField && (obj as TextField).type == TextFieldType.DYNAMIC)
				{
					obj = cloneTxt(obj as TextField);
					container.removeChildAt(idx);
					container.addChildAt(obj,idx);
				}
			}
		}
		
		/**
		 * 重绘显示对象内的文字对象，请在super()实例化父类后调用
		 * <br>示例</br>
		 * redrawTxt(demoDisplayObject,["neededRedrawText1","neededRedrawText2",...])
		 * @param param1 目标显示对象
		 * @param param2 需要重绘的文字对象名称合集
		 * @throws 目标容器内找不到需要重绘的文本框
		 *
		 */
		public static function redrawTxtOld(param1:DisplayObjectContainer, param2:Array):void
		{
			var idx:int;
			for each(var item:String in param2)
			{
				if (param1[item] && param1.contains(param1[item]))
				{
					idx = param1.getChildIndex(param1[item]);
					param1.removeChildAt(idx);
				}
				else
				{
					throw new Error("redraw textField err! Container must have contained this child!");
				}
				param1[item] = cloneTxt(param1[item]);
				param1.addChildAt(param1[item], idx);
			}
		}
		
		public static function getDefaultTextFormat(fontName:String = UNIVERSAL_FONT):TextFormat
		{
			var tf:TextFormat = new TextFormat();
			tf.font = fontName;
			tf.size = 20;
			tf.align = TextFormatAlign.CENTER;
			tf.color = 0x000000;
			return tf;
		}
		
		public static function setTextColor(txt:TextField, color:uint = 0):void
		{
			var tf:TextFormat = txt.getTextFormat();
			tf.color = color;
			txt.setTextFormat(tf);
		}
		
		public static function setTextAlign(txt:TextField, value:*):void
		{
			var tf:TextFormat = txt.getTextFormat();
			tf.align = value;
			txt.setTextFormat(tf);
		}

		public static function getTextBitmap(content:String,color:uint = 0xFF0000):Bitmap
		{
			var txt:TextField = new TextField();
			var tf:TextFormat = new TextFormat();
			tf.size = 16;
			tf.bold = true;
			tf.font = UNIVERSAL_FONT;
			tf.color = color;
			txt.embedFonts = true;
			txt.defaultTextFormat = tf;
			txt.text = content;
			txt.width = txt.textWidth + 10;
			txt = creatTextField(content,UNIVERSAL_FONT,16,TextFormatAlign.LEFT,color,false,false);
			FilterUtil.setGlow(txt,0xFFFFFF,1,4,4,5);
			FilterUtil.clearCache();
			var bmd:BitmapData = new BitmapData(txt.width, txt.height, true, 0xFFFFFF);
			bmd.draw(txt);
			return new Bitmap(bmd);
		}
		
		
		public static function GBK2UTF(param1:String):String
		{
			var ba:ByteArray = new ByteArray();
			ba.writeMultiByte(param1, " gbk ");
			ba.position = 0;
			return ba.readMultiByte(ba.length - 1," utf-8 ");
		}
		
		public static function UTF2GBK(param1:String):String
		{
			var ba:ByteArray = new ByteArray();
			ba.writeMultiByte(param1, "utf-8");
			ba.position = 0;
			return ba.readMultiByte(ba.length - 1," gbk ");
		}

	}
}
